unit ORM.ConsultObject;

interface

uses
  DBXCommon, System.Rtti, System.Generics.Collections, ORM.Base, ORM.Types;

type
  TConsultObject = class
  public
    class function LoadObject<T : class> : T; overload; static;
    class function LoadObject<T : class>( pObject : TObject ): T ; overload;  static;
    class function LoadObject<T : class>( pObject : TObject; pFilter : string ): T; overload; static;
    class function LoadObject<T : class>(const  pSQLCommand : string) : T; overload; static;

    class function LoadObjectList<T : class> : TObjectList<T>; overload;
    class function LoadObjectList<T : class>( pObject : TObject ): TObjectList<T>; overload; static;
    class function LoadObjectList<T : class>( pObject : TObject; pFilter : String ): TObjectList<T>; overload; static;
    class function LoadObjectList<T : class>( pObject : TObject; pFilter, pOrdination : String ): TObjectList<T>; overload; static;
    class function LoadObjectList<T : class>( pSQlCommand : string) : TObjectList<T>; overload; static;
    class function LoadObjectListForAnyField<T : class>( pObject : TObject ): TObjectList<T>; overload; static;

    class function GetValueInteger( const pConsulta: String): Integer; static;
    class function GetValueSring( const pConsulta :string ) : string; static;
  end;

implementation

uses
  System.SysUtils, ORM.Drive.Connection, ORM.Base.ObjectDBX;

{ TConsultObject }

class function TConsultObject.GetValueInteger(const pConsulta: String): Integer;
var
  comando : TDBXCommand;
  DBXReader: TDBXReader;
begin
  try
    comando :=  TConnectionDBX.GetConnection.DBXConnection.CreateCommand;
    comando.Text := pConsulta;
    DBXReader := comando.ExecuteQuery;
    if DBXReader.Next then
      Result :=  DBXReader.Value[0].AsInt32
    else
      Result := 0;
  finally
    FreeAndNil(comando);
    FreeAndNil(DBXReader);
  end;
end;

class function TConsultObject.LoadObjectList<T>( pObject: TObject; pFilter, pOrdination: String): TObjectList<T>;
var
  DBXReader : TDBXReader;
begin
  Result := nil;
  try
    DBXReader := TObjectDBX.GetDBXReader(pObject, pFilter, pOrdination);
    Result := TObjectDBX.LoadObjectListFromDBXReder<T>(DBXReader);
  finally
    FreeAndNil(DBXReader);
    TObjectDBX.DealocateCommand;
  end;
end;

class function TConsultObject.LoadObject<T>( pObject : TObject; pFilter : string): T;
var
  DBXReader : TDBXReader;
begin
  try
    DBXReader := TObjectDBX.GetDBXReader(pObject, pFilter);
    if DBXReader.Next then
      Result := TObjectDBX.LoadObjectFomDBXReader<T>(DBXReader);
  finally
    FreeAndNil(DBXReader);
    TObjectDBX.DealocateCommand;
  end;
end;

class function TConsultObject.LoadObjectListForAnyField<T>( pObject: TObject): TObjectList<T>;
var
  DBXReader: TDBXReader;
begin
  Result := nil;
  try
    DBXReader := TObjectDBX.ConsultForAnyField(pObject);
    Result := TObjectDBX.LoadObjectListFromDBXReder<T>(DBXReader);
  finally
    FreeAndNil(DBXReader);
    TObjectDBX.DealocateCommand;
  end;
end;

class function TConsultObject.LoadObjectList<T>( pObject: TObject; pFilter: String): TObjectList<T>;
var
  DBXReader : TDBXReader;
begin
  Result := nil;
  try
    DBXReader := TObjectDBX.GetDBXReader(pObject, pFilter);
    Result := TObjectDBX.LoadObjectListFromDBXReder<T>(DBXReader);
  finally
    FreeAndNil(DBXReader);
    TObjectDBX.DealocateCommand;
  end;
end;

class function TConsultObject.LoadObject<T>( pObject: TObject): T;
var
  DBXReader : TDBXReader;
begin
  try
    DBXReader := TObjectDBX.GetDBXReader(pObject);
    if DBXReader.Next then
      Result := TObjectDBX.LoadObjectFomDBXReader<T>(DBXReader)
  finally
    FreeAndNil(DBXReader);
    TObjectDBX.DealocateCommand;
  end;
end;

class function TConsultObject.LoadObjectList<T>( pObject: Tobject): TObjectList<T>;
var
  DBXReader: TDBXReader;
begin
  try
    DBXReader := TObjectDBX.GetDBXReader(pObject);
    Result := TObjectDBX.LoadObjectListFromDBXReder<T>(DBXReader);
  finally
    FreeAndNil(DBXReader);
    TObjectDBX.DealocateCommand;
  end;
end;

class function TConsultObject.GetValueSring(const pConsulta: string): string;
var
  Comando : TDBXCommand;
  DBXReader: TDBXReader;
begin
  try
    comando :=  TConnectionDBX.GetConnection.DBXConnection.CreateCommand;
    comando.Text := pConsulta;
    DBXReader := comando.ExecuteQuery;
    if DBXReader.Next then
      Result :=  DBXReader.Value[0].AsString
    else
      Result := EmptyStr;
  finally
    FreeAndNil(Comando);
    FreeAndNil(DBXReader);
  end;
end;

class function TConsultObject.LoadObject<T>( const pSQLCommand: string): T;
var
  Reader : TDBXReader;
begin
  try
    Reader := TObjectDBX.GetDBXReader(pSQLCommand);
    if Reader.Next then
      Result := TObjectDBX.LoadObjectFomDBXReader<T>(Reader)
  finally
    FreeAndNil(Reader);
  end;
end;

class function TConsultObject.LoadObject<T>: T;
var
  FObject : T;
  FReader : TDBXReader;
begin
  try
    FObject := TGeneric<T>.CreateObject;
    FReader := TObjectDBX.GetDBXReader(FObject);
    if FReader.Next then
      Result := TObjectDBX.LoadObjectFomDBXReader<T>(FReader);
  finally
    FreeAndNil(FReader);
    FreeAndNil(FObject);
    TObjectDBX.DealocateCommand;
  end;
end;

class function TConsultObject.LoadObjectList<T>( pSQlCommand: string): TObjectList<T>;
var
  FDbxReader : TDBXReader;
begin
  try
    FDbxReader := TObjectDBX.GetDBXReader(pSQlCommand);
    Result := TObjectDBX.LoadObjectListFromDBXReder<T>(FDbxReader);
  finally
    FreeAndNil(FDbxReader);
    TObjectDBX.DealocateCommand;
  end;
end;

class function TConsultObject.LoadObjectList<T>: TObjectList<T>;
var
  FObject : T;
  FReader : TDBXReader;
begin
  try
    FObject := TGeneric<T>.CreateObject;
    FReader := TObjectDBX.GetDBXReader(FObject);
    Result := TObjectDBX.LoadObjectListFromDBXReder<T>(FReader);
  finally
    FreeAndNil(FReader);
    FreeAndNil(FObject);
    TObjectDBX.DealocateCommand;
  end;
end;

end.
